package com.huang.controller;

import com.huang.dao.UserMapper;
import com.huang.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/login/{username}/{password}")
    public int userLogin(@PathVariable("username") String username,
                            @PathVariable("password") String password){
        Integer id = userMapper.queryUser(username);
        if (id!=null){
            User user = userMapper.queryUserById(id);
            if (user.getPassword().equals(password)){
                return 200;
            }else {
                return 405;//密码错误
            }
        }else {
            return 404;//用户错误
        }
    }
    @GetMapping("/user/select/{userName}")
    public User userSelectByUserName(@PathVariable("userName") String userName){
        User user = userMapper.queryUserByUserName(userName);
        return user;
    }
    @GetMapping("/user/selectBlog/{userName}")
    public List<User> userSelectBlogByUserName(@PathVariable("userName") String userName){
        List<User> users = userMapper.queryAllByUserName(userName);
        return users;
    }
    @GetMapping("/user/selectbyid/{id}")
    public User querryUserById(@PathVariable("id") Integer id){
        User user = userMapper.queryUserById(id);
        return user;
    }
    @PostMapping("/user/update")
    public String userUpdate(@RequestBody User user){
        int i = userMapper.updateUserById(user);
        if (i>0){
            return "success";
        }else {
            return "error";
        }
    }
    @PostMapping("/user/save")
    public String saveUser(@RequestBody User user){
        try {
            userMapper.saveUser(user);
            return "success";
        }catch (Exception e){
            return "error";
        }
    }

}

